package gui.data.D2;

import static util.Util.checkNumeric;
import gui.ErrorMessage;
import gui.MyFrame;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import util.NumericCheckWrapper;

import calcs.CircleCalculator;

public class CircleInputGui extends JPanel {

	JLabel radiusLbl, diameterLbl, circumferenceLbl, areaLbl, arcLbl,
			arcAreaLbl, arcAngleLbl;
	JTextField radiusInput, diameterInput, circumferenceInput, areaInput,
			arcInput, arcAreaInput, arcAngleInput;
	MyFrame myFrame;

	public CircleInputGui(MyFrame myFrame) {
		this.myFrame = myFrame;
		run();
	}

	public void run() {
		this.setLayout(new GridLayout(8, 2));
		this.setPreferredSize(new Dimension(500, 500));
		radiusLbl = new JLabel("Radius = ");
		diameterLbl = new JLabel("Diameter = ");
		circumferenceLbl = new JLabel("Circumference = ");
		areaLbl = new JLabel("Area = ");
		arcLbl = new JLabel("Sector arc length = ");
		arcAreaLbl = new JLabel("Sector area = ");
		arcAngleLbl = new JLabel("Sector angle (degrees) = ");
		radiusInput = new JTextField();
		diameterInput = new JTextField();
		circumferenceInput = new JTextField();
		areaInput = new JTextField();
		arcInput = new JTextField();
		arcAreaInput = new JTextField();
		arcAngleInput = new JTextField();
		this.add(radiusLbl);
		this.add(radiusInput);
		this.add(diameterLbl);
		this.add(diameterInput);
		this.add(circumferenceLbl);
		this.add(circumferenceInput);
		this.add(areaLbl);
		this.add(areaInput);
		this.add(arcLbl);
		this.add(arcInput);
		this.add(arcAreaLbl);
		this.add(arcAreaInput);
		this.add(arcAngleLbl);
		this.add(arcAngleInput);
		JButton calculate = new JButton("Calculate");
		calculate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				openResult();
			}
		});
		this.add(new JLabel());
		this.add(calculate);
	}

	private void openResult() {
		boolean error = false;
		JTextField[] textFields = {radiusInput, diameterInput, circumferenceInput, areaInput,
				arcInput, arcAreaInput, arcAngleInput};
		Double[] values = new Double[7];
		for(int i = 0; i<textFields.length; i++){
			NumericCheckWrapper res = checkNumeric(textFields[i].getText());
			if(!res.isCorrect()){
				error = true;
				new ErrorMessage("Check input!");
				break;
			}else{
				values[i] = res.getValue();
			}
		}
		
		if(error){
			error = false;
		}else{
			new CircleCalculator(values[0], values[1], values[2], values[3], values[4], values[5], values[6], myFrame);
		}
	}
}
